package com.example.Aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.lang.reflect.Modifier;
import java.text.SimpleDateFormat;

//@Component
//@Aspect
@Slf4j
public class SpringAopImpExecution {

    private static SimpleDateFormat df = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss");

    SpringAopImpExecution() {
        log.info("SpringAopDemo 超级拦截器已加载 拦截所有com.example.Bean下的所有方法");
    }

    @Pointcut("execution(* com.example.Bean..*.*(..))")
    private void aspectJMethod() {
    }

    @Before("aspectJMethod()")
    public void doBefore(JoinPoint joinPoint) {
        log.info("doBefore" + df.format(System.currentTimeMillis()));
        log.info(joinPoint.getSignature().getDeclaringTypeName() + "类的方法为:" + joinPoint.getSignature().getName() + "被调用");
        log.info("目标方法所属类的简单类名:" + joinPoint.getSignature().getDeclaringType().getSimpleName());
        log.info("目标方法声明类型:" + Modifier.toString(joinPoint.getSignature().getModifiers()));

        // 获取传入目标方法的参数
        Object[] args = joinPoint.getArgs();
        for (int i = 0; i < args.length; i++) {
            if (args[i] != null) {
                log.info("第{}个参数为{}", (i + 1), args[i].toString());
            } else {
                log.info("第{}个参数为{}", (i + 1), null);
            }
        }



    }

    @After(value = "aspectJMethod()")
    public void doAfter(JoinPoint joinPoint) {
        log.info("doBefore" + df.format(System.currentTimeMillis()));

    }

}
